package com.warmlight.voicepacket.adapter.picture;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.warmlight.voicepacket.MApplication;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.utils.ActivityUtil;
import com.warmlight.voicepacket.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import io.rong.imageloader.core.ImageLoader;

/**
 * Created by weiweijinpeng on 2018/9/10.
 */

public class PlayskinGirdAdapter extends BaseAdapter {


    private String[] files;

    private LayoutInflater mLayoutInflater;

    private Context mContext;


    List<Boolean> gif_array=new ArrayList<>();

    public PlayskinGirdAdapter(String[] files, Context context, List<Boolean> gif_array) {
        this.files = files;
        this.mContext = context;
        this.gif_array.addAll(gif_array);
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return files == null ? 0 : files.length;
    }

    @Override
    public String getItem(int position) {
        return files[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyGridViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new MyGridViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.gridview_item, parent, false);

            viewHolder.imageView = (SimpleDraweeView) convertView.findViewById(R.id.album_image);
            viewHolder.picture_gif_type = (ImageView) convertView.findViewById(R.id.picture_gif_type);
            if (gif_array.get(position)){

                viewHolder.picture_gif_type.setVisibility(View.VISIBLE);
            }else{
                viewHolder.picture_gif_type.setVisibility(View.GONE);
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MyGridViewHolder) convertView.getTag();
        }
        String pictureUrl = getItem(position);
        if (getCount() == 1) {
            viewHolder.imageView.setLayoutParams(new RelativeLayout.LayoutParams(400, 400));
        }

        if (getCount() == 2 ||getCount() == 4) {
            viewHolder.imageView.setLayoutParams(new RelativeLayout.LayoutParams(300, 300));
        }


        if (gif_array.get(position)){
            viewHolder.picture_gif_type.setVisibility(View.VISIBLE);

            Uri uri = Uri.parse(pictureUrl);

            DraweeController draweeController =
                    Fresco.newDraweeControllerBuilder()
                            .setUri(uri)
                            .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
                            .build();

            viewHolder.imageView.setController(draweeController);
        }else{
            viewHolder.picture_gif_type.setVisibility(View.GONE);
            UIUtils.loadImage(viewHolder.imageView,pictureUrl);

        }

        return convertView;
    }

    private static class MyGridViewHolder {
        SimpleDraweeView imageView;
        ImageView picture_gif_type;
    }

}
